package com.whut.work.user.service.impl;

import com.whut.work.base.model.Page;
import com.whut.work.course.dao.impl.CourseDaoImpl;
import com.whut.work.course.model.Course;
import com.whut.work.user.dao.impl.UserDaoImpl;
import com.whut.work.user.model.User;
import com.whut.work.user.service.IUserNewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zouy on 17-3-15.
 */
@Component
public class UserNewServiceImpl implements IUserNewService {

    @Autowired
    private UserDaoImpl userDao;
    @Autowired
    private CourseDaoImpl courseDao;

    public Page<User> getUserPageList(int currentPage, int pageSize, String blurUserName) throws Exception {
        String queryHql = "from User u where u.username like '%"+blurUserName+"%' ";
        String countHql = "select count(*) from User u where u.username like '%"+blurUserName+"%' ";
        Page<User> returnPage = userDao.findPage(currentPage, pageSize, queryHql, countHql);

        return returnPage;
    }

    public Map<String, Object> getCoursePageListByUserId(int currentPage, int pageSize, Integer userId) throws Exception {
        Map<String,Object> returnMap = new HashMap<String,Object>();
        String hql = "from User u where u.id='"+userId+"' ";
        User user = userDao.findOne(hql);
        if(user != null){
            returnMap.put("value", user);
            returnMap.put("message", "获取成功");
            returnMap.put("success", true);
        }else {
            returnMap.put("message", "错误");
            returnMap.put("success", false);
        }

        return returnMap;
    }

    public Map<String, Object> getInfoByUserId(Integer userId) throws Exception {
        Map<String,Object> returnMap = new HashMap<String,Object>();
        User user = userDao.findOne("from User u where u.id='"+userId+"' ");
        if(user != null){
            returnMap.put("value", user);
            returnMap.put("message", "获取成功");
            returnMap.put("success", true);
        }else {
            returnMap.put("message", "错误");
            returnMap.put("success", false);
        }

        return returnMap;
    }

    public Page<User> getTeacherPageList(int currentPage, int pageSize, Integer leaderId) throws Exception {
        String queryHql = "from User u where u.parentId='"+leaderId+"' ";
        String countHql = "select count(*) from User u where u.parentId='"+leaderId+"' ";
        Page<User> returnPage = userDao.findPage(currentPage, pageSize, queryHql, countHql);

        return returnPage;
    }

}
